package com.dailywork.basepro.thread.message;

import java.util.LinkedList;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class MyQueue {

    // 1.��Ҫһ������Ԫ�صļ���
    LinkedList<Object> list = new LinkedList<Object>();

    // 2.��Ҫһ��������(ͳ��linkedList�ĸ���)
    private AtomicInteger count = new AtomicInteger(0);
    // Ӧ������ConcurrentHashMap����������,��10��(���ݸ����ݽṹ,�ʵ��޸�ҵ���߼�)
    private ConcurrentHashMap<String, ConcurrentHashMap<String, Integer>> map =
            new ConcurrentHashMap<String, ConcurrentHashMap<String, Integer>>();

    // 3.��Ҫ��Լ���޺�����
    private final int minSize = 0;
    private int maxSize;

    // 4.���췽��
    public MyQueue(int size) {
        this.maxSize = maxSize;
    }

    // 5.��ʼ��һ������,���ڼ���
    private final Object lock = new Object();

    // put(anObject):��anObject�ӵ�BlockingQueue��,���BlockingQueueû�пռ�,����ô˷������̱߳����,ֱ��BlockingQueue�����пռ��ټ���
    public void put(Object obj) {
        synchronized (lock) {
            while (count.get() == this.maxSize) {
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            // 1.����Ԫ��
            list.add(obj);
            // 2.�������ۼ�
            count.incrementAndGet(); // ��1��notify
            // ֪ͨ����һ���߳�(����)
            lock.notify();
            System.out.println("�¼����Ԫ��Ϊ: " + obj);
        }
    }

    // take:
    // ȡ��BlockingQueue��������λ�Ķ���,��BlockingQueueΪ��,��Ͻ���ȴ�״̬,ֱ��BlockingQueue���µ����ݱ�����
    public Object take() {
        Object ret = null;
        synchronized (lock) {
            while (count.get() == this.maxSize) {
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            // 1.���Ƴ�Ԫ�ز���
            ret = list.removeFirst();
            // 2.�������ݼ�
            count.decrementAndGet();
            // 3.��������һ���߳�
            lock.notify();
        }
        return ret;
    }

    public int getSize() {
        return this.count.get();
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        final MyQueue mq = new MyQueue(5);
        mq.put("a");
        mq.put("b");
        mq.put("c");
        mq.put("d");
        mq.put("e");

        System.out.println("��ǰ�����ĳ���: " + mq.getSize());
        Thread t1 =
                new Thread(
                        new Runnable() {

                            @Override
                            public void run() {
                                // TODO Auto-generated method stub
                                mq.put("f");
                                mq.put("g");
                            }
                        },
                        "t1");

        t1.start();

        Thread t2 =
                new Thread(
                        new Runnable() {

                            @Override
                            public void run() {
                                // TODO Auto-generated method stub
                                Object o1 = mq.take();
                                System.out.println("�Ƴ���Ԫ��Ϊ: " + o1);
                                Object o2 = mq.take();
                                System.out.println("�Ƴ���Ԫ��Ϊ: " + o2);
                            }
                        },
                        "t2");

        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        t2.start();
    }
}
